<!DOCTYPE html>
<html lang="zh-cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="renderer" content="webkit">
    <title></title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
</head>
<body>
<form id="form" class="layui-form-pane">
    <input type="hidden" id="rid" name="rid" th:value="${rid}" />
    <div id="test1"></div>
    <br/>
    <div class="layui-form-item layui-inline" style="float: right; margin-right: 10px;">
        <button class="layui-btn" id="addPerms">提交</button>
        <button class="layui-btn layui-btn-danger" onclick="closePage()">关闭</button>
    </div>
</form>
</body>
<script th:src="@{/js/jquery-2.1.0.js}"></script>
<script th:src="@{/layui/layui.js}"></script>
<script th:inline="javascript">
    var aaa=[[${tree}]];
    // console.log(aaa);
    getCheckedId(aaa)
    // alert(getCheckedId(aaa));
    var index = parent.layer.getFrameIndex(window.name);
    layui.use('form','layer',function(){
        var form = layui.form;
        form.render();
    });
    layui.use('layer',function(){
        var layer=layui.layer;
    });
    layui.use('tree', function(){
        var tree = layui.tree;

        //渲染
        var inst1 = tree.render({
            elem: '#test1'  //绑定元素
            ,showLine: true//连接线
            ,onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
            ,showCheckbox: true
            ,data:aaa
            ,id:'demo1'
        });

        $('#addPerms').click(function(){

            var checkedData = tree.getChecked('demo1'); //获取选中节点的数据
            console.log(checkedData);
            var nodeIds="";
            nodeIds = getCheckedId(checkedData);
            console.log(nodeIds);
            // alert(nodeIds);
            $.ajax({
                type:'post',
                url:'/role/savePerms',
                data:{'rid':$("#rid").val(),'mids':nodeIds},
                dataType: "text",
                async: false,
                success:function (data) {
                    if (data === 1){
                    }
                }
            });
            window.parent.location.reload();//刷新父页面
            parent.layer.close(index);
        });
    });
    //获取所有选中的节点id
    function getCheckedId(data) {
        var id = "";
        $.each(data, function (index, item) {
            if (id != "") {
                id = id + "," + item.id;
            }
            else {
                id = item.id;
            }
            //item 没有children属性
            /*if (item.children != null) {
                var i = getCheckedId(item.children);
                if (i != "") {
                    alert(id);
                    id = id + "," + i;
                }
            }*/
            var i = getCheckedId(item.children);
            if (i != "") {
                id = id + "," + i;
            }
        });
        return id;
    }



    function closePage() {
        parent.layer.close(index);//关闭当前页
    }


</script>
</html>